package com.myrpc.listener;

import com.myrpc.transfer.transport.socket.SocketRpcServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class MyRpcListener implements ApplicationListener<ApplicationStartedEvent> {
    @Autowired
    SocketRpcServer socketRpcServer;


    /**
     * Handle an application event.
     *
     * @param event the event to respond to
     */
    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        log.info("My Rpc 监听到事件[{}]，即将启动...", event.getSource());
        System.out.println(
                "      ___           ___           ___           ___           ___     \n" +
                        "     /\\__\\         |\\__\\         /\\  \\         /\\  \\         /\\  \\    \n" +
                        "    /::|  |        |:|  |       /::\\  \\       /::\\  \\       /::\\  \\   \n" +
                        "   /:|:|  |        |:|  |      /:/\\:\\  \\     /:/\\:\\  \\     /:/\\:\\  \\  \n" +
                        "  /:/|:|__|__      |:|__|__   /::\\~\\:\\  \\   /::\\~\\:\\  \\   /:/  \\:\\  \\ \n" +
                        " /:/ |::::\\__\\     /::::\\__\\ /:/\\:\\ \\:\\__\\ /:/\\:\\ \\:\\__\\ /:/__/ \\:\\__\\\n" +
                        " \\/__/~~/:/  /    /:/~~/~    \\/_|::\\/:/  / \\/__\\:\\/:/  / \\:\\  \\  \\/__/\n" +
                        "       /:/  /    /:/  /         |:|::/  /       \\::/  /   \\:\\  \\      \n" +
                        "      /:/  /     \\/__/          |:|\\/__/         \\/__/     \\:\\  \\     \n" +
                        "     /:/  /                     |:|  |                      \\:\\__\\    \n" +
                        "     \\/__/                       \\|__|                       \\/__/    \n"
        );
        //启动Rpc请求监听
        Thread thread = new Thread(()->{
            log.info("启动Rpc请求监听");
            socketRpcServer.start();
        }, "RpcRequestListenThread");
        thread.start();
    }
}
